package com.bookstore.admin.notice.handler;

import com.bookstore.admin.notice.service.INoticeService;
import com.bookstore.commons.beans.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/admin/notices")
public class noticehandler {

    @Autowired
    INoticeService noticeService;


    @RequestMapping("/ListNoticeServlet")
    public String ListNoticeServlet(Model model){
        List<Notice> notices = noticeService.findAllNotice();
       /*  for (Notice n:notices){
             System.out.println(n);
         }*/
         model.addAttribute("notices",notices);
        return "/admin/notices/list.jsp";
    }

    /*查找公告ID*/
    @RequestMapping("/FindByIdNoticeServlet")
    public String FindByIdNoticeServlet(String flag,Integer id,Notice notice ,Model model){
        if (flag == null){
            notice = noticeService.findNoticeById(id);
            System.out.println(notice);
            model.addAttribute("n",notice);
            return "/admin/notices/edit.jsp";
        }
        else {
            int rows = noticeService.modifyNotice(notice);
            System.out.println(rows);
            if (rows > 0){
                return "/admin/notices/ListNoticeServlet";
            }else {
                return "/admin/notices/error.jsp";
            }
        }
    }

    @RequestMapping("/AddNoticeServlet")
    public String AddNoticeServlet(Notice notice){
        int rows = noticeService.AddNotices(notice);
        if (rows > 0){
            return "redirect:/admin/notices/ListNoticeServlet";
        }
        else {
            return "/admin/notices/error.jsp";
        }
    }


    @RequestMapping("/DeleteNoticeServlet")
    public String DeleteNoticeServlet(int id){
         int rows = noticeService.removeNotice(id);
         if (rows > 0){
             return "redirect:/admin/notices/ListNoticeServlet";
         }
         else {
             return "/admin/notices/error.jsp";
         }
    }
}
